package service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import dao.InspTaskDetailDao;
import entity.MapInfo;
import entity.Properties;
import entity.PsInspectiontaskDetail;

@Service("inspTaskDetailService")
public class InspTaskDetailService<K, V> {
	@Autowired
	@Qualifier("inspTaskDetailDao")
	private InspTaskDetailDao inspTaskDetailDao;

	public MapInfo show(int page, int rows,
			PsInspectiontaskDetail psInspTaskDetail,Date discoverStartTime,Date discoverEndTime) {
		MapInfo lineInfo = new MapInfo();
		lineInfo.setRows(inspTaskDetailDao.selectAllLine(page, rows,
				addCriteria(psInspTaskDetail,discoverStartTime,discoverEndTime)));
		lineInfo.setTotal(inspTaskDetailDao
				.count(addCriteria(psInspTaskDetail,discoverStartTime,discoverEndTime)));
		return lineInfo;
	}

	private DetachedCriteria addCriteria(PsInspectiontaskDetail psInspTaskDetail,Date discoverStartTime,Date discoverEndTime) {
		DetachedCriteria searchDc = DetachedCriteria
				.forClass(PsInspectiontaskDetail.class);
		if (psInspTaskDetail != null) {
			System.out.println(psInspTaskDetail.getTaskid());
			System.out.println(psInspTaskDetail.getLinecode());
			System.out.println(psInspTaskDetail.getPolecode());
			System.out.println(psInspTaskDetail.getDiscoverercode());
			System.out.println(psInspTaskDetail.getBuglevelname());
			System.out.println(psInspTaskDetail.getBuglevel());
			System.out.println(discoverEndTime);
			System.out.println(discoverStartTime);
			if (psInspTaskDetail.getTaskid() != null) {
				searchDc.add(Restrictions.eq("taskid", psInspTaskDetail.getTaskid()));
			}
			if (!"".equals(psInspTaskDetail.getLinecode())) {
				searchDc.add(Restrictions.eq("linecode", psInspTaskDetail.getLinecode()));
			}
			if (!"".equals(psInspTaskDetail.getPolecode())) {
				searchDc.add(Restrictions.eq("polecode", psInspTaskDetail.getPolecode()));
			}
			if (!"".equals(psInspTaskDetail.getDiscoverercode())) {
				searchDc.add(Restrictions.eq("discoverercode", psInspTaskDetail.getDiscoverercode()));
			}
			if (!"".equals(psInspTaskDetail.getBuglevelname())) {
				searchDc.add(Restrictions.eq("buglevelname", psInspTaskDetail.getBuglevelname()));
			}
			if (psInspTaskDetail.getBuglevel()!= null) {
				searchDc.add(Restrictions.eq("buglevel", psInspTaskDetail.getBuglevel()));
			}
			if (discoverEndTime!=null&&discoverStartTime!=null) {
				searchDc.add(Restrictions.between("discovertime", discoverStartTime, discoverEndTime));
			}else
			if (discoverStartTime!=null) {
				searchDc.add(Restrictions.eq("discovertime", discoverStartTime));
			}else
			if (discoverEndTime!=null) {
				searchDc.add(Restrictions.eq("discovertime", discoverEndTime));
			}
			
		}
		searchDc.add(Restrictions.eq("buglevel", 0));
		return searchDc;
	}

	public boolean changeBugLever(PsInspectiontaskDetail psInspTaskDetail) {
		try {
			inspTaskDetailDao.changeBugLever(psInspTaskDetail);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

}
